




####——————————————————————生存曲线篇——————————————————————
library(survival);library(survminer);library(ggplot2)
data("lung");head(lung)
setwd('E:/RCloud/R learning metirial/sample document/survival')
fit <- survfit(Surv(time, status) ~ sex, data = lung);print(fit)#r
summary(fit)#进行数据整理，按不同的性别，按time进行排序。

###以数据框的形式对fit进一步展示：
d <- data.frame(time = fit$time,
                n.risk = fit$n.risk,
                n.event = fit$n.event,
                n.censor = fit$n.censor,
                surv = fit$surv,
                upper = fit$upper,
                lower = fit$lower);head(d)


###绘制和保存生存曲线,观察sex对肺癌生存时间有无影响。
lung_suvival <- ggsurvplot(fit,
                           pval = TRUE, conf.int = TRUE,
                           risk.table = F,
                           risk.table.col = "strata",
                           linetype = "strata",
                           surv.median.line = "hv",
                           palette = c("#E7B800", "#2E9FDF"))
p <- lung_suvival$plot + #此处一定要用$plot，才可以用“+”运算并且最后可以用ggsave保存。
  theme(legend.justification=c(1,0),legend.position=c(0.95,0.85),
        axis.text.x = element_text(size = 10,colour = "black",face = "bold"),axis.text.y = element_text(size = 10,colour = "black",face = "bold"),#修改坐标轴标签属性
        axis.title.x = element_text(size = 15,colour = "black",face = "bold"),axis.title.y = element_text(size = 15,colour = "black",face = "bold")) + 
  scale_y_continuous(expand = c(0,0)) + 
  scale_x_continuous(expand = c(0,0)) #坐标轴原点重叠
dev.new();p
ggsave(p,file = 'lung cancer_suvival1.pdf')


##或者可以用%++%来连接，但此法无法以ggsave保存：
lung_suvival <- ggsurvplot(fit,
                           pval = TRUE, conf.int = TRUE,#加上P值和置信区间
                           risk.table = F, # Add risk table
                           risk.table.col = "strata", # Change risk table color by groups
                           linetype = "strata", # Change line type by groups
                           surv.median.line = "hv", # Specify median survival
                           palette = c("#E7B800", "#2E9FDF"),
                           ggtheme = theme_bw()) %++%
  theme(legend.justification=c(1,0),legend.position=c(0.95,0.85),panel.border = element_blank(),
        panel.grid.major = element_blank(),panel.grid.minor = element_blank(),#去网格去边框
        axis.line = element_line(colour = "black"),
        axis.text.x = element_text(size = 10,colour = "black",face = "bold"),axis.text.y = element_text(size = 10,colour = "black",face = "bold"),#修改坐标轴标签属性
        axis.title.x = element_text(size = 15,colour = "black",face = "bold"),axis.title.y = element_text(size = 15,colour = "black",face = "bold")) %++% 
  scale_y_continuous(expand = c(0, 0)) #坐标轴原点重叠
dev.new();lung_suvival


##检验显著性,rho=0为log-rank法或Mantel Haenszel法，rho=1为Wilcoxon法
survdiff(Surv(time,status)~sex, data=lung,rho = 0)
survdiff(Surv(time,status)~sex, data=lung,rho = 1)
#或者设置不同函数的不同参数，选择不同的检验方法。此例，全部检验均显示p value均小于0.05，提示差异有统计学意义。
survreg(Surv(time,status)~sex, data=lung,dist="weibull")
survreg(Surv(time,status)~sex, data=lung,dist="logistic")
survreg(Surv(time,status)~sex, data=lung,dist="lognormal")


##累积风险(cumulative hazard):随着时间的推进，累积风险
p_ch <- ggsurvplot(fit,
                   conf.int = TRUE,
                   risk.table.col = "strata",
                   ggtheme = theme_bw(),
                   palette = c("#E7B800", "#2E9FDF"),
                   fun = "cumhaz")
dev.new();p_ch
ggsave(p_ch$plot,file = 'lung cancer_cumulative hazard.pdf')

##logrank回归：
surv_diff <- survdiff(Surv(time, status) ~ sex, data = lung);surv_diff


###其它生存分析的图像：
##一、获得某段时间的生存分析曲线：
dev.new();ggsurvplot(fit,
                     conf.int = TRUE,
                     risk.table.col = "strata", # Change risk table color by groups
                     ggtheme = theme_bw(), # Change ggplot2 theme
                     palette = c("#E7B800", "#2E9FDF"),
                     xlim = c(0, 600))#获得某段时间的生存分析曲线

##将图形翻转过来：
dev.new();ggsurvplot(fit,
                     conf.int = TRUE,
                     risk.table.col = "strata", # Change risk table color by groups
                     ggtheme = theme_bw(), # Change ggplot2 theme
                     palette = c("#E7B800", "#2E9FDF"),
                     fun = "event")



##对于colon，根据不同的sex和rx（6组合），观察不同的adhere的差异。如图，
require("survival")
data(colon);head(colon)
fit2 <- survfit( Surv(time, status) ~ sex + rx + adhere,
                 data = colon )#根据不同的sex和rx（6组合），观察不同的adhere的差异。
ggsurv <- ggsurvplot(fit2, fun = "event", conf.int = TRUE,
                     ggtheme = theme_bw())
dev.new();ggsurv$plot + theme_bw() + theme (legend.position = "right") + 
  facet_grid(rx ~ adhere)#分面函数






